<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
set_html('title','商户积分商城列表');
if($_SESSION['onemerchurl']){
	exi('','',$_SESSION['onemerchurl']);
}
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$total='';
	$jy_merch = $this->plugin_exists('jy_merch');
	if($jy_merch){
		$return['merchgroups']=pdo_getslice('jy_merch_category',$params,array($pindex,$psize),$total,$fields,'','id desc');
		$merch_types=$this->merch_types2;
		$return['merch_types']=array();
		foreach($merch_types as $k=>&$mv){
			$childs=pdo_getall('jy_merch_category',array('type'=>$k,'uniacid'=>$_SESSION['uniacid']));
			$return['merch_types'][]=array(
					'id'=>$k,
					'title'=>$mv,
					'childs'=>$childs,
			);
		}
	}
	$return['basic']['searchkeyword']=$return['basic']['searchkeyword']?explode('|',$return['basic']['searchkeyword']):array();
	$return['list']=array();
	$return['params']=array('cid'=>intval($_RQ['cid']));
	exi($return);
}
if($_RQ['op']=='getmerchs'){
	$jy_merch = $this->plugin_exists('jy_merch');
	if(!$jy_merch){
		exi(array('list'=>array()));
	}
	//获取最近50家店
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	$lat_lng=$this->session('lat_lng');
	$params=array(
		'uniacid'=>$_SESSION['uniacid'],
		'status'=>1,
		'merchname <>'=>'',
		'lat_lng <>'=>'',
	);
	if($_RQ['cid']){
		$params['cateid']=intval($_RQ['cid']);
		$return['params']['cid']=intval($_RQ['cid']);
	}
	$orderby='isrecommand desc, salenum desc, id desc';
	if(trim($_RQ['order']) && trim($_RQ['order'])!='all'){
		$order=trim($_RQ['order']);
		if($order=='salenum'){
			$orderby='salenum desc, isrecommand desc, id desc';
			$orderbykey='salenum';
		}
		if($order=='sudu'){
			$orderby='sudu desc, isrecommand desc, salenum desc, id desc';
			$orderbykey='sudu';
		}
		if($order=='star'){
			$orderby='star desc, isrecommand desc, salenum desc, id desc';
			$orderbykey='star';
		}
		if($order=='brand'){
			$params['isbrand']=1;
		}
		if($order=='you'){
			$orderby='isrecommand desc, salenum desc, id desc';
			$orderbykey='isrecommand';
		}
		if($order=='out_pay'){
			$orderby='out_pay desc, isrecommand desc, salenum desc, id desc';
			$orderbykey='out_pay';
		}
		if($order=='out_price'){
			$orderby='out_price desc, isrecommand desc, salenum desc, id desc';
			$orderbykey='out_price';
		}
		$return['params']['order']=trim($_RQ['order']);
	}
	if(intval($_RQ['star'])){
		$params['star >']=intval($_RQ['star'])-0.2;
		$return['params']['star']=intval($_RQ['star']);
	}
	if(trim($_RQ['keyword'])){
		$params['merchname like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}
	$pindex = max(1, 1);
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,10000),//获取所有
		'fields'=>array('id','merchname','out_price','lat_lng','logo','topimg','cateid','onlinetime'),
		'key'=>'id',
		'orderby'=>$orderby,
		'orderbykey'=>$orderbykey,
	);
	$credit_types=credit_types(true);
	$merchs=model('jy_merch/model')->getmerch($parameter,$_RQ['limit'],$lat_lng,intval($_RQ['page']),intval($_RQ['juli']));
	foreach($merchs as $k=>&$v){
			$v['goodsnum']=pdo_count('jy_creditshop_goods',array('merchid'=>$v['id']),'id');
			$v['logo']=$v['logo']?tomedia($v['logo']):'';
			$v['topimg']=$v['topimg']?tomedia($v['topimg']):'';
			$v['onlinetime']=json_decode($v['onlinetime'],true);
			$v['onlinetime']['start']=$v['onlinetime']['start']?:'7:00';
			$v['onlinetime']['end']=$v['onlinetime']['end']?:'22:00';	
			$days=date('Y-m-d',TIMESTAMP);
			$star=strtotime($days.' '.$v['onlinetime']['start']);
			$end=strtotime($days.' '.$v['onlinetime']['end']);
			if($star>TIMESTAMP || TIMESTAMP>$end){
				$v['offline']=1;
			}
			if($v['juli']<10){
				$v['peisongtime']=$v['juli']*5+28;
			}else if($v['juli']<30){
				$v['peisongtime']=$v['juli']*3+28;
			}else{
				$v['peisongtime']=$v['juli']*1+28;
			}
			$pindex = max(1, 1);
			$psize = 6;
			$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'merchid'=>$v['id']);
			$fields=array('id','title','price','money','thumb','credit','credit_type','is_moretype','creditmore');
			$goods=pdo_getslice('jy_creditshop_goods',$params,array($pindex,$psize),$total,$fields,'','displayorder desc, id desc');
			foreach($goods as &$gv){
					if($item['is_moretype']){
						$item['creditmore']=$item['creditmore']?json_decode($item['creditmore'],true):array();
						$item['credit']=$item['creditmore'][0]['credit'];
						$item['credit_title']=$credit_types[$item['creditmore'][0]['credit_type']];
					}else{
						$item['credit_title']=$credit_types[$item['credit_type']];
					}
			}
			$v['goods']=$goods;
			$v['salenum']=pdo_getsum('jy_creditshop_log',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$v['id']),'total');		
	}
	$return['list']=array_values($merchs);
	exi($return);
}
?>